package com.baibei.ebec.guide;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baibei.crossboarder.R;
import com.baibei.ebec.adapter.GuidePagerAdapter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GuideActivity extends AppCompatActivity implements View.OnClickListener {

    @BindView(R.id.vp_guide)
    ViewPager vpGuide;
//    @BindView(R.id.btn_pre)
//    Button btnPre;
//    @BindView(R.id.btn_next)
//    Button btnNext;
//    @BindView(R.id.ll_dot)
//    LinearLayout llDot;

    private int[] pics = {R.layout.guide_view1, R.layout.guide_view2, R.layout.guide_view3};
    private TextView btnEnter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);
        int currentVersionCode = getVersionCode();
        SharedPreferences sp = getSharedPreferences("version", MODE_PRIVATE);
        int versionCode = sp.getInt("versionCode", 0);

        if (currentVersionCode == versionCode){
            toAdvert();
            return;
        }
        String[] permissions = new String[]{
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.CAMERA
        };

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, permissions, 777);
        }

        SharedPreferences.Editor editor = sp.edit();
        editor.putInt("versionCode",currentVersionCode);
        editor.commit();

        ArrayList<View> mViewList = new ArrayList<>();
        for (int i = 0;i < pics.length; i++){
            View view = getLayoutInflater().from(this).inflate(pics[i], null);
            if (i == pics.length - 1){
                btnEnter = view.findViewById(R.id.btn_enter);
                btnEnter.setTag("enter");
                btnEnter.setOnClickListener(this);
            }
            mViewList.add(view);
        }

        GuidePagerAdapter guidePagerAdapter = new GuidePagerAdapter(mViewList);
        vpGuide.setAdapter(guidePagerAdapter);

    }

    private int getVersionCode() {
        PackageManager packageManager = getPackageManager();
        try {
            PackageInfo info = packageManager.getPackageInfo(getPackageName(), 0);
            return info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }

//    @OnClick({R.id.btn_pre, R.id.btn_next})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.btn_pre:
//                break;
//            case R.id.btn_next:
//                break;
//        }
//    }

    @Override
    public void onClick(View view) {
        if (view.getTag().equals("enter")){
            toAdvert();
        }
    }

    private void toAdvert() {
        Intent intent = new Intent(GuideActivity.this,AdvertisementActivity.class);
        startActivity(intent);
        finish();
    }
}
